package flirtme.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ans.flirtme.R;

import flirtme.activities.ShowProfileActivity;
import flirtme.constants.Constants;
import flirtme.libraries.imageloader.ImageLoader;
import flirtme.model.ContactModel;

public class ContactListAdapter extends BaseAdapter {

	private ArrayList<ContactModel> listData;
	private LayoutInflater layoutInflater = null;
	private ImageLoader imageLoader = null; 
	private Context context;
	
	public ContactListAdapter(Context _context, ArrayList<ContactModel> listData) {
		this.listData = listData;
		layoutInflater = LayoutInflater.from(_context);
		imageLoader = new ImageLoader(_context.getApplicationContext());
		context = _context;
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
		
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.list_row_contact, null);
			holder = new ViewHolder();
			holder.username = (TextView) convertView.findViewById(R.id.username);
			holder.distance = (TextView) convertView.findViewById(R.id.distance);
			holder.profileButton =(ImageButton) convertView.findViewById(R.id.thumb_button); // thumb image
			holder.numberOfMessagesNotReaded = (TextView)convertView.findViewById(R.id.messagesNotReaded);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		final ContactModel model = listData.get(position);
		holder.username.setText(model.getUsername());
		holder.distance.setText("Distance: " + model.getDistance() + "m.");
		imageLoader.DisplayImage(model.getImageUrl(), holder.profileButton);
		
		int numberOfMessages = model.getNumberOfMessagesNotReaded();
		String numberOfMessagesString =  String.valueOf(numberOfMessages);
		numberOfMessagesString = numberOfMessages > 9 ?  9 + "+" : numberOfMessagesString;
		holder.numberOfMessagesNotReaded.setText(numberOfMessagesString);
		
	
		holder.profileButton.setFocusable(false);
		holder.profileButton.setTag(model.getUsername());
				
		return convertView;
	}

	static class ViewHolder {
		TextView numberOfMessagesNotReaded;
		TextView username;
		TextView distance;
		ImageButton profileButton;
	}

}
